//
//  VISTAPictureVC.m
//  看杂志
//
//  Created by zcj on 15-12-14.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "VISTAPictureVC.h"
#import "VISTASettingVC.h"
#import "VISTAPictureCell.h"
#import "SelectFindHttp.h"
#import "Grouble.h"
#import "VISTAPictureViewModel.h"
#import "VISTAPictureDetailVC.h"


@interface VISTAPictureVC ()<UITableViewDelegate,UITableViewDataSource>
{
 UICollectionView *_pictureView;
    int stat;
}
@property (nonatomic, strong)NSMutableArray *dataSourceArray;
@end

@implementation VISTAPictureVC
- (NSMutableArray *)dataSourceArray
{
    if (_dataSourceArray == nil) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configViewAndNavi];
    [self createCollectionView];
    [self loadData];
    // Do any additional setup after loading the view.
}
// 加载数据
- (void)loadData
{
    NSString *path = [NSString stringWithFormat:@"http://ktx.cms.palmtrends.com/api_v2.php?action=piclist&sa=&uid=10067567&mobile=iphone5&offset=%ld&count=%ld&&e=40dab97d773e7860febfc897c04824e2&uid=10067567&pid=10053&mobile=iphone5&platform=i",(long)0,(long)12];
    ZCJLog(@"%@",path);
    [SelectFindHttp postWirhUrl:path parms:nil success:^(id json) {
        [self.dataSourceArray removeAllObjects];
        NSArray * list = json[@"list"];
        for (NSDictionary * dict in list) {
            VISTAPictureViewModel * pictureModel = [VISTAPictureViewModel objectWithKeyValues:dict];
            [self.dataSourceArray addObject:pictureModel];
        }
        ZCJLog(@"%d",self.dataSourceArray.count);
        [_pictureView reloadData];
        [MBProgressHUD showSuccess:@"加载完成"];
    } failture:^(id error) {
        ZCJLog(@"%@",error);
    }];
}
- (void)configViewAndNavi
{
    //导航栏背景
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NAVI"] forBarMetrics:UIBarMetricsDefault];
    //导航栏标题View
    UIImageView *titleImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo"]];
    self.navigationItem.titleView = titleImageView;
    //导航栏右按钮
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 35, 35);
    [rightButton setImage:[UIImage imageNamed:@"设置_1"] forState:UIControlStateNormal];
    [rightButton setImage:[UIImage imageNamed:@"设置_2"] forState:UIControlStateHighlighted];
    [rightButton addTarget:self action:@selector(goToSettingVC) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    //ViewController背景图片
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"资讯背景底"]];
}
- (void)goToSettingVC
{
    VISTASettingVC *settingVC = [[VISTASettingVC alloc]init];
    UINavigationController *setNavi = [[UINavigationController alloc]initWithRootViewController:settingVC];
    [self presentViewController:setNavi animated:YES completion:nil];
}
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width-2)/3, 125);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 30, 0);
    
    _pictureView = [[UICollectionView alloc]initWithFrame:CGRectMake(1, 0, self.view.frame.size.width-2, self.view.frame.size.height) collectionViewLayout:flowLayout];
    _pictureView.backgroundColor = [UIColor clearColor];
    _pictureView.dataSource = self;
    _pictureView.delegate = self;
    [_pictureView registerClass:[VISTAPictureCell class] forCellWithReuseIdentifier:@"pictureID"];
    [self.view addSubview:_pictureView];
}
#pragma mark - collectionDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    VISTAPictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pictureID" forIndexPath:indexPath];
    if (stat % 2 == 0) {
        cell.bg_imageView.image = [UIImage imageNamed:@"酷图底2"];
    }else{
        cell.bg_imageView.image = [UIImage imageNamed:@"酷图底1"];
    }
    [cell fillCellWithModel:self.dataSourceArray[indexPath.item]];
    NSLog(@"加载cell中");
    stat ++;
    if (stat == 4) {
        stat = 1;
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    VISTAPictureDetailVC *picDetailVC = [[VISTAPictureDetailVC alloc]init];
    picDetailVC.indexPathRow = indexPath.row;
    picDetailVC.dataSourceArray = self.dataSourceArray;
    [self.navigationController pushViewController:picDetailVC animated:YES];
}



@end
